<?xml version='1.0' encoding='UTF-8'?>



<!--
 * default.xsl
 * 
 * AUTHOR:
 * Zlatko Michailov (http://blogs.msdn.com/esql)
 * 
 * PURPOSE:
 * Transformation of an XML serialization of a hierarchical DbDataReader to HTML.
 * 
 * COPYRIGHT:
 * (c) Microsoft Corp., 2006-2007
-->



<xsl:stylesheet version='1.0'
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
	xmlns:msxsl='urn:schemas-microsoft-com:xslt'
	xmlns:esql='http://msdn.microsoft.com/data/esql'>


	<msxsl:script language='JavaScript' implements-prefix='esql'>
		function text2html(text, nle, spe, lt, lte)
		{
			var l;
			var i;
			var c;
			var html;
			
			html = '';
			l = text.length;
			for (i = 0; i &lt; l; i++)
			{
				c = text.charAt(i);
				switch (c)
				{
					case '\r':
						break;
						
					case '\n':
						html += nle;
						break;

					case ' ':
						html += spe;
						break;
						
					case lt:
						html += lte;
						break;
						
					default:
						html += c;
						break;
				}
			}
				
			return html.valueOf();
		}
	</msxsl:script>

	<xsl:template match='/'>
		<html>
			<head>
				<style>
          body, table
          {
          font-family: Verdana;
          font-size: 10pt;
          color: black;
          background: white;
          }

          th
          {
          font-weight: bold;
          background: #c0c0c0;
          }

          .collection
          {
          background: #c0e0ff;
          }

          .entity
          {
          background: #ffe0e0;
          }

          .struct
          {
          background: #ffffc0;
          }

          .ref
          {
          background: #e0ffe0;
          }

          .row
          {
          background: #ffe0ff;
          }

          .null
          {
          font-weight: bold;
          color: brown;
          }

          .code
          {
          font-family: Courier New;
          font-size: 11pt;
          background: #f0f0f0;
          }

          .error
          {
          font-family: Courier New;
          font-size: 11pt;
          color: red;
          background: #ffffd0;
          border-color: red;
          }
        </style>
			</head>
			<body>
				<xsl:apply-templates select='Batch' />
			</body>
		</html>
	</xsl:template>

	
	<xsl:template match='Command'>
		<xsl:apply-templates select='EntityStatement' />
		<xsl:apply-templates select='StoreStatement' />
		<xsl:apply-templates select='Exception' />
		<xsl:call-template name='RecordCount' />
		<xsl:apply-templates select='Collection' />
		<br />
		<br />
		<br />
		<br />
		<br />
	</xsl:template>

	
	<xsl:template match='EntityStatement'>
		<table border='1'>
      <tr>
        <th>Entity Command</th>
      </tr>
			<tr>
				<td class='code'>
          <xsl:value-of disable-output-escaping='yes' select='esql:text2html(string(.), "&lt;br /&gt;", "&amp;nbsp;", "&lt;", "&amp;lt;")' />
				</td>
			</tr>
		</table>
		<br />
	</xsl:template>


	<xsl:template match='StoreStatement'>
		<table border='1'>
      <tr>
        <th>Store Command</th>
      </tr>
			<tr>
				<td class='code'>
					<xsl:value-of disable-output-escaping='yes' select='esql:text2html(string(.), "&lt;br /&gt;", "&amp;nbsp;", "&lt;", "&amp;lt;")' />
				</td>
			</tr>
		</table>
		<br />
	</xsl:template>


  <xsl:template match='Exception'>
    <table border='1'>
      <tr>
        <td class='error'>
          <xsl:value-of disable-output-escaping='yes' select='esql:text2html(string(.), "&lt;br /&gt;", "&amp;nbsp;", "&lt;", "&amp;nbsp;")' />
        </td>
      </tr>
    </table>
    <br />
  </xsl:template>


  <xsl:template name='RecordCount'>
    <table border='1'>
      <tr>
        <th>Record Count</th>
      </tr>
      <tr>
        <td class='code'>
          <xsl:value-of disable-output-escaping='yes' select='count(Collection/Record)' />
        </td>
      </tr>
    </table>
    <br />
  </xsl:template>


  <xsl:template match='Collection'>
		<table border='1'>
			<tr>
				<xsl:for-each select='Record[position() = 1]/*'>
					<th class='collection'>
						<xsl:value-of select='name()' />
					</th>
				</xsl:for-each>
			</tr>

      <xsl:for-each select='Record' >
			  <xsl:call-template name='Record' />
      </xsl:for-each>
		</table>
	</xsl:template>


  <xsl:template match='Entity'>
    <table border='1'>
      <tr>
        <xsl:for-each select='*'>
          <th class='entity'>
            <xsl:value-of select='name()' />
          </th>
        </xsl:for-each>
      </tr>

      <xsl:call-template name='Record' />
    </table>
  </xsl:template>


  <xsl:template match='Struct'>
    <table border='1'>
      <tr>
        <xsl:for-each select='*'>
          <th class='struct'>
            <xsl:value-of select='name()' />
          </th>
        </xsl:for-each>
      </tr>

      <xsl:call-template name='Record' />
    </table>
  </xsl:template>


  <xsl:template match='Ref'>
          <table border='1'>
            <tr>
              <th class='ref'>
                EntitySet
              </th>

              <xsl:for-each select='*'>
                <th class='ref'>
                  <xsl:value-of select='@Name' />
                </th>
              </xsl:for-each>
            </tr>
            <tr>
              <td>
                <xsl:value-of select='@EntitySet' />
              </td>

              <xsl:for-each select='*'>
                <td>
                  <xsl:value-of select='@Value' />
                </td>
              </xsl:for-each>
            </tr>
          </table>
  </xsl:template>


  <xsl:template match='Row'>
    <table border='1'>
      <tr>
        <xsl:for-each select='*'>
          <th class='row'>
            <xsl:value-of select='name()' />
          </th>
        </xsl:for-each>
      </tr>

      <xsl:call-template name='Record' />
    </table>
  </xsl:template>


  <xsl:template name='Record'>
		<tr>
      <xsl:for-each select='*'>
				<td>
          <xsl:if test='boolean(@Null)' >
            <font color='brown'>
              <b>NULL</b>
            </font>
          </xsl:if>

          <xsl:if test='count(Collection) > 0'>
            <xsl:apply-templates select='Collection' />
          </xsl:if>

          <xsl:if test='count(Entity) > 0' >
            <xsl:apply-templates select='Entity' />
          </xsl:if>

          <xsl:if test='count(Struct) > 0' >
            <xsl:apply-templates select='Struct' />
          </xsl:if>

          <xsl:if test='count(Ref) > 0' >
            <xsl:apply-templates select='Ref' />
          </xsl:if>

          <xsl:if test='count(Row) > 0' >
            <xsl:apply-templates select='Row' />
          </xsl:if>

          <xsl:if test='count(Collection) = 0 and count(Entity) = 0 and count(Struct) = 0 and count(Ref) = 0 and count(Row) = 0'>
            <xsl:value-of select='.' />
          </xsl:if>
				</td>
			</xsl:for-each>
    </tr>
	</xsl:template>


</xsl:stylesheet>
